package com.java.service.impl;

import com.java.entity.User;
import com.java.entity.UserExample;
import com.java.mapper.UserMapper;
import com.java.service.AuthService;
import com.java.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        UserExample example = new UserExample();
        example.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(example);
        if (users.size() < 1) {
            throw new RuntimeException("Login failed");
        }
        return users.get(0);
    }

    @Override
    public boolean verify(String token) {
        return JwtUtils.verifyToken(token);
    }

}
